import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";

import http from '../../http/index'


const initialState: { value: {} } = {
  value: {},
};



export const fetchGroupMemberInfo: any = createAsyncThunk('/group/member/info', async (id: any) => {
  const mapStr = localStorage.getItem("group_member_map");
  const groupMemberMap = mapStr ? JSON.parse(mapStr) : {};
  const response = await http.get(`/group/members?id=${id}`);
  if(response.data){
    groupMemberMap[`${id}`] = response.data;
  }

  return groupMemberMap;
})


export const groupMemberSlice = createSlice({
  name: "groupMember",
  initialState,
  reducers: {

  },
  extraReducers: (builder) => {
    builder.addCase(fetchGroupMemberInfo.fulfilled, (state, action) => {
     const payload: any = action.payload; 
     state.value = payload;
     localStorage.setItem("group_member_map", JSON.stringify(payload));
    })
  },
})

export const { } = groupMemberSlice.actions;

export default groupMemberSlice.reducer